package com.example.studentinfo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Change extends AppCompatActivity {
    EditText et_xm;
    EditText et_xh;
    EditText et_bj;
    EditText et_zy;
    EditText et_cj;
    Button btn_confirm;
    RadioGroup rb_xb;
    Button del;
    Intent priIntent;
    AlertDialog.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        setTitle("ChangeStudentInfo");
        priIntent = getIntent();//显示意图接受要修改的学生信息
        builder = new AlertDialog.Builder(Change.this);
        getinfo();
        btn_confirm = findViewById(R.id.btn_change);
        del=findViewById(R.id.btn_del);
        setlisener();

    }

    /**
     * 获取MainActivity传来的修改信息显示到输入框
     */
    public void getinfo(){
        et_xm = findViewById(R.id.et_xm);
        rb_xb=findViewById(R.id.radioGroup);
        et_xh = findViewById(R.id.et_xh);
        et_bj = findViewById(R.id.et_bj);
        et_zy = findViewById(R.id.et_zy);
        et_cj = findViewById(R.id.et_cj);
        String prixm = priIntent.getStringExtra("xm");
        String prixb = priIntent.getStringExtra("xb");
        String prixh = priIntent.getStringExtra("xh");
        String pribj = priIntent.getStringExtra("bj");
        String prizy = priIntent.getStringExtra("zy");
        String pricj = priIntent.getStringExtra("cj");
        et_xm.setText(prixm);
        if(prixb.equals("男")){
            rb_xb.check(R.id.btnMan);
        }
        else{
            rb_xb.check(R.id.btnWoman);
        }
        et_xh.setText(prixh);
        et_bj.setText(pribj);
        et_zy.setText(prizy);
        et_cj.setText(pricj);
    }
    /**
     * 设置监听
     */
    public void setlisener(){
        String priId = priIntent.getStringExtra("id");
        RadioGroup radgroup = findViewById(R.id.radioGroup);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String xm = et_xm.getText().toString();
                RadioButton radbtn=findViewById(radgroup.getCheckedRadioButtonId()) ;
                String xb=radbtn.getText().toString();
                String xh = et_xh.getText().toString();
                String bj = et_bj.getText().toString();
                String zy = et_zy.getText().toString();
                String cj = et_cj.getText().toString();
                if (TextUtils.isEmpty(xm) || TextUtils.isEmpty(xh)) {
                    Toast.makeText(Change.this, "学号或者姓名不可为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("_id", priId);
                intent.putExtra("xm", xm);
                intent.putExtra("xb",xb);
                intent.putExtra("xh", xh);
                intent.putExtra("bj", bj);
                intent.putExtra("zy", zy);
                intent.putExtra("cj", cj);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                builder.setTitle("删除确认").setMessage("是否确认删除该学生");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.dbHelper.delStudent(priId);
                        Toast.makeText(Change.this,"删除成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Change.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
                builder.show();
            }
        });
    }
}